using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BuffEditorMenu : MonoBehaviour {
	
	public UILabel buffName;
	public UILabel buffDescription;
	public GameObject AddModifierButton;
	public GameObject ModifierEntryPrefab;
	
	public GameObject CancelButton;
	public GameObject AcceptButton;
	
	Dictionary<string, TemporaryBonus> Modifiers;
	List<GameObject> modifierObjects;
	
	public UITable holder;
	
	public GameObject target;
	public string functionName;
	
	string origName;
	
	static GameReference _reference;
	
	static GameReference reference
	{
		get
		{
			if(_reference == null)
				_reference = GameObject.FindObjectOfType(typeof(GameReference)) as GameReference;
			return _reference;
		}
	}
	
	bool Loaded = false;

	// Use this for initialization
	void Start () {	
			UIButtonMessage message = AddModifierButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "AddModifierClicked";
		
		ModifierEntryPrefab.gameObject.SetActiveRecursively(false);
		
		if(!Loaded)
		{			
			message = CancelButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "CancelButtonClicked";
			
			message = AcceptButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "AcceptButtonClicked";
			
			Modifiers = new Dictionary<string, TemporaryBonus>();
		}
		
		holder.repositionNow = true;
	}
	
	public void LoadBuffDisplay(string bName, bool noEdit = false)
	{
		Modifiers = new Dictionary<string, TemporaryBonus>();
		
		BuffDebuff b = BuffDebuffManager.Load(bName);
		
		origName = bName;
		
		buffName.text = bName;
		buffDescription.text = b.description;
		
		Loaded = true;
		
		CreateModifierObjects(b.modifiers, noEdit);
		
		if(noEdit)//!isCustom && Application.platform != RuntimePlatform.WindowsEditor)
		{
			foreach(UIInput l in gameObject.GetComponentsInChildren<UIInput>())
			{
				Destroy(l);
			}
			
			CancelButton.SetActiveRecursively(false);
			AddModifierButton.SetActiveRecursively(false);
			
			UIButtonMessage message = AcceptButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "CancelButtonClicked";
		}
		else
		{
			foreach(UIInput l in gameObject.GetComponentsInChildren<UIInput>())
			{
				l.text = l.label.text;
				l.mDefaultText = "Some Info";
			}
			
			UIButtonMessage message = AcceptButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "SaveChangesClicked";
			
			CancelButton.GetComponentInChildren<UILabel>().text = "Delete!";
			message = CancelButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "DeleteClicked";
		}
	}

	
	void CreateModifierObjects(List<TemporaryBonus> selections, bool noEdit = false)
	{
		if(selections != null)
		{
			foreach(TemporaryBonus bonus in selections)
			{
				Modifiers.Add(bonus.toStat + (string.IsNullOrEmpty(bonus.subStat)?"":" ("+bonus.subStat+")"), bonus);
			}
		}
		
		foreach(string i in Modifiers.Keys)
		{
			GameObject modLabel = Instantiate(ModifierEntryPrefab) as GameObject;
			modLabel.transform.parent = holder.transform;
			modLabel.transform.localPosition = Vector3.zero;
			modLabel.transform.localScale = Vector3.one;
			modLabel.GetComponentsInChildren<UILabel>()[1].text = i;
			modLabel.GetComponentsInChildren<UILabel>()[2].text = ((Modifiers[i].amount >= 0)?"+":"") + ((Modifiers[i].dicep > 0)?Modifiers[i].dicep+"d":"") + Modifiers[i].amount;
			modLabel.GetComponentsInChildren<UILabel>()[3].text = Modifiers[i].type + " Bonus";
			modLabel.name = "c. " + i;
			
			if(!noEdit)
			{
				UIButtonMessage message = modLabel.AddComponent<UIButtonMessage>();
				message.target = gameObject;
				message.functionName = "DeleteModifierListLabel";
			}
		}
		
		holder.repositionNow = true;
	}
	
	void DeleteModifierListLabel(GameObject target)
	{
		Debug.Log("Deleting: " + target);
		target.name = target.name.Remove(0,3);
		Modifiers.Remove(target.name);
		Destroy(target);
		
		holder.repositionNow = true;
	}
	
	IEnumerator AddModifierClicked()
	{
		List<SetterWindowDescription> typeVar = new List<SetterWindowDescription>
		{
			new SetterWindowDescription("Type", "Type", false, CharacterSheet.validModifications)
		};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow("Select Modifier Stat", typeVar));
		
		List<SetterWindowDescription> descVar = new List<SetterWindowDescription>
		{
			new SetterWindowDescription("Bns. Type", "Misc.", true),
			new SetterWindowDescription("Amount", "2", true)
		};
		
		if(typeVar[0].outVariable == null)
		{
			return false;
		}
		
		if(typeVar[0].outVariable == "Skill")
		{
			descVar.Add(new SetterWindowDescription("Skill", "Acrobatics", true));
		}
		
		yield return StartCoroutine(HelperFunctions.SetterWindow("Set Modifier Info", descVar));

		if(descVar[0].outVariable != null && typeVar[0].outVariable != null)
		{
			//int i;
			int ii;
			string[] dice;
			int dieCount = 0;
			
			if(descVar[1].outVariable.Contains("d"))
			{
				dice = descVar[1].outVariable.Split('d');
				descVar[1].outVariable = dice[1];
				
				if(dice != null && int.TryParse(dice[0], out ii))
				{
					dieCount = ii;
				}
			}
			
			if(!Modifiers.ContainsKey(typeVar[0].outVariable))
			{
				TemporaryBonus t = new TemporaryBonus();
				t.toStat = typeVar[0].outVariable;
				t.type = descVar[0].outVariable;
				t.dicep = dieCount;
				t._amount = descVar[1].outVariable;
				
				if(typeVar[0].outVariable == "Skill")
				{
					t.subStat = descVar[2].outVariable;
				}

				Modifiers.Add(t.toStat + (string.IsNullOrEmpty(t.subStat)?"":" ("+t.subStat+")"), t);
				
				GameObject modLabel = Instantiate(ModifierEntryPrefab) as GameObject;
				modLabel.transform.parent = holder.transform;
				modLabel.transform.localPosition = Vector3.zero;
				modLabel.transform.localScale = Vector3.one;
				modLabel.GetComponentsInChildren<UILabel>()[1].text = t.toStat + (string.IsNullOrEmpty(t.subStat)?"":" ("+t.subStat+")");
				if(t.dicep > 0)
					modLabel.GetComponentsInChildren<UILabel>()[2].text = t.dicep + "d" + t.amount;
				else
					modLabel.GetComponentsInChildren<UILabel>()[2].text = ((t.amount >= 0)?"+":"") + t.amount;
				modLabel.GetComponentsInChildren<UILabel>()[3].text = t.type + " Bonus";
				modLabel.name = "c. " + t.toStat;
				UIButtonMessage message = modLabel.AddComponent<UIButtonMessage>();
				message.target = gameObject;
				message.functionName = "DeleteModifierListLabel";
			}
			
			holder.repositionNow = true;
		}
	}
	
	void SaveChangesClicked()
	{
		BuffDebuff b = new BuffDebuff();
		b.name = buffName.text;
		b.description = buffDescription.text;
		b.modifiers = new List<TemporaryBonus>();
		
		foreach(TemporaryBonus t in Modifiers.Values)
		{
			b.modifiers.Add(t);
		}
		
		if(BuffDebuffManager.SaveBuffDebuff(b, origName))
		{
			target.SendMessage("renamedObject", new KeyValuePair<string, string>(origName, b.name), SendMessageOptions.DontRequireReceiver);
			
			Destroy(gameObject);
		}
	}
	
	void CancelButtonClicked()
	{
		Destroy(gameObject);
	}
	
	IEnumerator DeleteClicked()
	{
		List<bool> b = new List<bool>{false};
		
		yield return StartCoroutine(HelperFunctions.ConfirmationWindow("Are you sure you want to delete " + origName + "?", b));
		
		if(b[0])
		{
			BuffDebuffManager.DeleteBuffDebuff(origName);
			
			if(target != null)
				target.SendMessage(functionName);
		}
		 
		Destroy(gameObject);
		yield break;
	}
	
	void AcceptButtonClicked()
	{
		BuffDebuff b = new BuffDebuff();
		b.name = buffName.text;
		b.description = buffDescription.text;
		b.modifiers = new List<TemporaryBonus>();
		
		foreach(TemporaryBonus t in Modifiers.Values)
		{
			b.modifiers.Add(t);
		}
		
		b = BuffDebuffManager.AddBuffDebuff(b.name, b.description, b.modifiers);
		
		if(target != null)
			target.SendMessage(functionName, new KeyValuePair<string,string>(b.name, b.description), SendMessageOptions.DontRequireReceiver);
		
		Destroy(gameObject);
	}
}
